package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Task;
import com.util.DBHelp;
import com.util.RowMapper;

public class TaskDao {
	
	DBHelp<Task> help=new DBHelp<Task>();

	public List<Task> getTaskByEid(String id) {
		String sql="SELECT id,`name`,`desc`,state,`level`,begintime,endtime,createtime,employeeid,goalid,progress FROM t_task WHERE employeeid=? ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), id);
	}
	
	private class Mapper implements RowMapper<Task>{

		public Task mapperRow(ResultSet rs) throws SQLException {
			Task task=new Task();
			task.setBegintime(rs.getString("begintime"));
			task.setCreatetime(rs.getString("createtime"));
			task.setDesc(rs.getString("desc"));
			task.setEmployeeid(rs.getString("employeeid"));
			task.setEndtime(rs.getString("endtime"));
			task.setGoalid(rs.getString("goalid"));
			task.setLevel(rs.getString("level"));
			task.setName(rs.getString("name"));
			task.setState(rs.getString("state"));
			task.setRate(rs.getInt("rate"));
			task.setId(rs.getString("id"));
			
			return task;
		}
		
	}

	public void addTask(Task task) {
		String sql="INSERT INTO `t_task` (id,`name`,`desc`,`state`,`level`,`begintime`,`endtime`,`createtime`,`employeeid`,`goalid`) VALUES(?,?,?,?,?,?,?,?,?,?)";
		help.executeSQL(sql,task.getId() , task.getName(),task.getDesc(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getCreatetime(),task.getEmployeeid(),task.getGoalid());
	}

	public Task getTaskById(String id) {
		String sql="SELECT id,`name`,`desc`,state,`level`,begintime,endtime,createtime,employeeid,goalid,rate FROM t_task WHERE id=?";
		return help.executeQueryForObject(sql, new Mapper(), id);
	}

	public void updateTask(Task task) {
		String sql="UPDATE t_task SET `state`=?,`rate`=? WHERE id=?";
		help.executeSQL(sql, task.getState(),task.getRate(),task.getId());
	}

	public List<Task> getAll() {
		String sql="SELECT `id`,`name`,`desc`,`state`,`level`,`begintime`,`endtime`,`createtime`,`employeeid`,`goalid`,`rate` FROM `t_task` ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper());
	}

}
